﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using NLBS.Entity;

namespace NLBS.Data
{
    public partial class SqlDataProvider : DbProvider
    {
        #region FriendLink

        public override IList<FriendLink> GetLinks(LinkItemType linkType)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("SELECT link_ID, link_Name, link_Url, link_Image, link_IsMain, link_Order, link_IsArrow FROM dbo.blog_Link");

            IList<FriendLink> lists = new List<FriendLink>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    FriendLink link = CommonDataProvider.PopulateFriendLinkFromIDataReader(dataReader);
                    lists.Add(link);
                }
            }
            return lists;
        }

        public override FriendLink GetLinkByID(int linkID)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand(@"SELECT link_ID, link_Name, link_Url, link_Image, link_IsMain, link_Order, link_IsArrow
            FROM            blog_Link
            WHERE        (link_ID = @linkid)");
            db.AddInParameter(dbCommand, "linkID", DbType.Int32, linkID);

            FriendLink link = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    link = CommonDataProvider.PopulateFriendLinkFromIDataReader(dataReader);
                }
            }
            return link;
        }

        public override void InsertLink(FriendLink link)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("INSERT INTO [dbo].[blog_Link] ([link_Name], [link_Url], [link_Image], [link_IsMain], [link_Order], [link_IsArrow]) VALUES (@link_Name, @link_Url, @link_Image, @link_IsMain, @link_Order, @link_IsArrow)");
            db.AddInParameter(dbCommand, "link_Name", DbType.String, link.LinkName);
            db.AddInParameter(dbCommand, "link_Url", DbType.String, link.LinkURL);
            db.AddInParameter(dbCommand, "link_Image", DbType.String, link.LinkLogo);
            db.AddInParameter(dbCommand, "link_IsMain", DbType.DateTime, link.IsMain);
            db.AddInParameter(dbCommand, "link_IsArrow", DbType.String, link.IsArrow);
            db.AddInParameter(dbCommand, "link_Order", DbType.String, link.LinkOrder);
            db.ExecuteNonQuery(dbCommand);
        }

        public override int UpdateLink(FriendLink link)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("UPDATE [dbo].[blog_Link] SET [link_Name] = @link_Name, [link_Url] = @link_Url, [link_Image] = @link_Image, [link_IsMain] = @link_IsMain, [link_Order] = @link_Order, [link_IsArrow] = @link_IsArrow WHERE (([link_ID] = @link_ID))");
            db.AddInParameter(dbCommand, "link_ID", DbType.Int32, link.LinkID);
            db.AddInParameter(dbCommand, "link_Name", DbType.String, link.LinkName);
            db.AddInParameter(dbCommand, "link_Url", DbType.String, link.LinkURL);
            db.AddInParameter(dbCommand, "link_Image", DbType.String, link.LinkLogo);
            db.AddInParameter(dbCommand, "link_IsMain", DbType.Boolean, link.IsMain);
            db.AddInParameter(dbCommand, "link_IsArrow", DbType.Boolean, link.IsArrow);
            db.AddInParameter(dbCommand, "link_Order", DbType.Int32, link.LinkOrder);
            return db.ExecuteNonQuery(dbCommand);
        }

        public override int DeleteLink(int linkID)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand("DELETE FROM [dbo].[blog_Link] WHERE (([link_ID] = @link_ID))");
            db.AddInParameter(dbCommand, "link_ID", DbType.String, linkID);
            return db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}
